package com.example.curriculum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;


public class MessageActivity extends Activity implements OnItemClickListener,OnScrollListener{

    private ListView listView;
    private ArrayAdapter<String> arr_adapter;
    private SimpleAdapter simp_adapter;
    String arr[] = {"互动消息","新的打招呼","官方推送","系统通知"};
    private List<Map<String, Object>>dataList;//SimpleAdapter的data
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        listView=(ListView) findViewById(R.id.listView);
        //1.新建一个数据适配器
        //ArrayAdapter（上下文，当前listView加载的每一个列表项所对应的布局文件，数据源）
        //2.适配器加载数据源
        String[]arr_data={"互动消息","新的打招呼","官方推送","系统通知"};
        arr_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_data);
        dataList=new ArrayList<Map<String,Object>>();
        simp_adapter=new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text});
        listView.setAdapter(simp_adapter);
        listView.setOnItemClickListener(this);
        listView.setOnScrollListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Intent intent = new Intent(MessageActivity.this, hudongActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        Toast.makeText(MessageActivity.this,"暂时没新的打招呼", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(MessageActivity.this,"暂时没新的推送", Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(MessageActivity.this,"暂时没新的通知", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    private List<Map<String, Object>> getData() {
        for(int i=0;i<4;i++){
            Map<String, Object>map=new HashMap<String, Object>();
            map.put("pic", R.drawable.tuisong);
            map.put("text", arr[i]);
            dataList.add(map);
        }
        return dataList;
    }
    @Override
    public void onScroll(AbsListView view, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        switch (scrollState) {
            case SCROLL_STATE_FLING:
                Log.i("Main","用户在手指离开屏幕之前，由于用力划了一下，试图仍依靠惯性继续滑动");
                Map<String, Object>map=new HashMap<String, Object>();//下拉刷新
                map.put("pic", R.drawable.zhong);//下拉刷新
                map.put("text", "增加项");//下拉刷新
                dataList.add(map);//下拉刷新
                simp_adapter.notifyDataSetChanged();//下拉刷新，动态更新视图中所包含的数据
                break;
            case SCROLL_STATE_IDLE:
                Log.i("Main","视图已经停止滑动");
            case SCROLL_STATE_TOUCH_SCROLL:
                Log.i("Main","手指没有离开屏幕，视图正在滑动");
                break;
            default:
                break;
        }
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
    }
}